import axios from "./request"
import { IErrorData, ISuccessData, ISuccessPageData } from "./returnData"

// 请求类型
export interface BlogPageQuery {
    limit?: number,
    page?: number,
    keyword?: string,
    categoryId?: string
}

//获得数据类型
export interface BlogDataObj {
    id?: string,
    title: string,
    description: string,
    createDate?: number,
    scanNumber?: number,
    commentNumber?: number,
    htmlContent?: string,
    category: {
        id: string,
        name: string
    },
    toc: any[]
    thumb: string
}

// 文章数据
export interface IBlogData {
    rows: BlogDataObj[],
    blogTotal: number,//当前分类的数量
    total: number//总数量
}

//文章分类数据
export interface IBlogType {
    name: string,
    articleCount: number,
    order: number,
    id: string
}

/**
 * 更具分页获取文章内容
 * @param param0 
 * @returns 
 */
export function getBlog({ page = 1, limit = 10, keyword = "", categoryId = "" }: BlogPageQuery): Promise<ISuccessPageData<IBlogData> | IErrorData> {
    console.log(page, limit)
    return axios.get("/api/blog", {
        params: {
            page,
            limit,
            keyword,
            categoryId
        }
    })
}

/**
 * 获取单篇文章的内容
 * @param id id值
 * @returns 
 */
export function getBlogDetail(id: string): Promise<ISuccessPageData<BlogDataObj> | IErrorData> {
    return axios.get(`/api/blog/${id}`);
}


/**
 * 修改文章
 * @param id 文章id
 * @param data 修改的内容
 * @returns 
 */
export function updateBlog(id: string, data: any): Promise<ISuccessData<BlogDataObj> | IErrorData> {
    return axios.put(`/api/blog/${id}`, data);
}

/**
 * 添加文章
 */

export function addblog(data: Partial<BlogDataObj>): Promise<ISuccessPageData<BlogDataObj> | IErrorData> {
    return axios.post('/api/blog', data)
}

/**
 * 删除文章
 */
export function deleteBlog(id: string): Promise<ISuccessPageData<boolean> | IErrorData> {
    return axios.delete(`/api/blog/${id}`);
}

/**
 * 获取文章分类
 * @returns 
 */
export function getBlogTypes(): Promise<ISuccessData<IBlogType> | IErrorData> {
    return axios.get('/api/blogtype');
}

/**
 * 添加文章分类
 * @param type 文章分类
 * @returns 
 */
export function addBlogType(type: Partial<IBlogType>): Promise<ISuccessData<IBlogType> | IErrorData> {
    return axios.post('/api/blogtype', type)
}

/**
 * 删除文章分类
 * @param id 文章分类id值
 * @returns 
 */
export function deleteBlogType(id: string): Promise<ISuccessPageData<number> | IErrorData> {
    return axios.delete(`/api/blogtype/${id}`);
}